package cn.platform.util;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.platform.util.identify.AllInterceptor;
import cn.platform.util.identify.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册一个全局拦截器，只是用于查看访问者信息，没有实际业务用途
        registry.addInterceptor(new AllInterceptor());

        // 拦截器进行身份验证
        registry.addInterceptor(new TokenInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/api/user/**");
    }

    //====================================== 注册异常处理器 ====================================================
    @Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        resolvers.add(customExceptionResolver());
    }

    @Bean
    public HandlerExceptionResolver customExceptionResolver() {
        return new CustomHandlerExceptionResolver();
    }
    //====================================== 注册异常处理器 ====================================================
}
